package com.cjl.demo.juc;

import java.util.concurrent.CountDownLatch;

/**
 * <p>Copyright: All Rights Reserved</p>
 * <p>Description: 当一个或多个线程调用await方法时,调用线程会被阻塞.
 * 其他线程调用countDown方法计数器减1(调用countDown方法时线程不会阻塞),
 * 当计数器的值变为0,因调用await方法被阻塞的线程会被唤醒,继续执行
 *</p>
 *
 * @ uthor chenjinliang
 * @ date: 2020/2/16
 */
public class CountDownLatchDemo {
    public static void main(String[] args) throws Exception {
        closeDoor();

    }

    /**
     * 关门案例
     *
     * @throws InterruptedException
     */
    private static void closeDoor() throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "\t" + "上完自习");
                countDownLatch.countDown();
            }, String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + "\t班长锁门离开教室");
    }

}
